package com.moehu.back.controller.base;

import com.alibaba.fastjson.JSONObject;
import com.moehu.back.annotation.AppRestController;
import com.moehu.back.annotation.UserLoginToken;
import com.moehu.back.model.*;
import com.moehu.back.model.request.UpdateListRequest;
import com.moehu.back.model.response.PagingResponseModel;
import com.moehu.back.service.impl.ToolServiceImpl;
import com.moehu.back.utils.AliYunOSS.AliyunOSSUtil;
import com.moehu.back.utils.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping(value = "/tool")
@AppRestController
@Api(tags = "Tool 公共类接口")
public class ToolController {

    @Autowired
    ToolServiceImpl toolService;

    @ApiOperation(value="文件上传", notes="用来上传图片，文件等，前端$(\"input\")[0].files[0]后将数据append进new FormData()中，然后使用ajax上传。")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件的files数据，以FormData格式上传", required = true, dataType = "FormData"),
    })
    @PostMapping("/UploadFile")
    public ResultModel UploadFile(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return ResultUtils.Success("请选择文件");
        }
        String uploadUrl = AliyunOSSUtil.upLoad(file);
        JSONObject json = new JSONObject();
        json.put("img_url", uploadUrl);
        return ResultUtils.Success(json);
    }


    @ApiOperation(value="短信验证码", notes="用于向用户发送短信验证码，注意阿里云短信审核未通过，请勿调用该接口，但是前端短信验证码请传固定值：1314")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "用户手机号", required = true, dataType = "String"),
    })
    @PostMapping("/SmsCode")
    public ResultModel SmsCode(String phone) {
        return ResultUtils.Success("json");
    }


    @ApiOperation(value="【app端使用】app升级检测", notes="后端返回最新的app版本，前端使用字段：update_version_code 和本地app的code进行对比，决定是否需要升级，同时根据update_force 字段决定是否强制升级，update_force取值：1 需要强制升级，0 不需要")
    @PostMapping("/Update")
    public ResultModel<UpdateModel> Update() {
        UpdateListRequest page = new UpdateListRequest();
        return toolService.getNewUpdate("one", page);
    }

    @ApiOperation(value="【app端使用】记录版本的下载次数", notes="当前端用户点击升级弹窗的 确定 或 下载 按钮时调用这个接口")
    @PostMapping("/addUpdateCount")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "update_id", value = "从 /Update 接口中获取", required = true, dataType = "String"),
    })
    public ResultModel<String> addUpdateCount(@RequestBody UpdateModel update) {
        return toolService.addUpdateCount(update);
    }

}
